﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfeccionesJR.BussinesLogic;
using ConfeccionesJR.Entites;
using Microsoft.Practices.Unity;

namespace ConfeccionesJR.UI
{
    public partial class frmPrimerPago : Form
    {
        [Dependency]
        public IClienteBL clienteBL { get; set; }

        [Dependency]
        public IPagoBL pagoBL { get; set; }

        [Dependency]
        public ICreditoBL creditoBL { get; set; }

        public frmPrimerPago()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void frmPrimerPago_Load(object sender, EventArgs e)
        {
        }

        bool isNew = true;
        public void nuevoPago(int creditoId, int clienteId)
        {
            cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteById(clienteId);
            cREDITOBindingSource.DataSource = creditoBL.GetFromCreditoById(creditoId);
            dETALLEPAGOesBindingSource.AddNew();
        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            var currentPago = dETALLEPAGOesBindingSource.Current as DETALLEPAGO;
            var currentCredito = cREDITOBindingSource.Current as CREDITO;
            var msg = "ERROR";

            if (currentCredito != null && importeTextBox.Text != "" && importeTextBox.Text != string.Empty)
            {
                if (isNew == true)
                {
                    try
                    {
                        var pago = Convert.ToDecimal(importeTextBox.Text);
                        if (pago <= currentCredito.Total)
                        {
                            currentPago.CreditoId = currentCredito.Id;
                            currentPago.Descripcion = "Primer pago hecho a las " + Convert.ToString(DateTime.Now.ToLongTimeString());
                            currentPago.Fecha = DateTime.Today.Date;
                            currentPago.Monto = Convert.ToDecimal(importeTextBox.Text);
                            pagoBL.AddPago(currentPago);
                            MessageBox.Show("Pago registrado", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("El monto a pagar no puede ser mayor a la deuda", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("Debe ingresar un monto válido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void importeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) &&!char.IsDigit(e.KeyChar) &&e.KeyChar != (char)Keys.Back &&
                e.KeyChar != (char)Keys.Enter)
            {
                e.Handled = true;
            }

            if (e.KeyChar == '.' || e.KeyChar == '\b')
            {
                e.Handled = false;
            }
        }


    }
}
